package com.googlecode.yonggemovie.widgets.imagelist;

import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

/** 
 * ImageList渲染器  
 */
public class ImageListCellRenderer extends JLabel implements ListCellRenderer {

	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean hasFocus) {

		ImageListData data = (ImageListData) value;

		//在JLabel上放上图标和字符串 并设置文本在右边,以及两者的间距	 
		setText(data.getText());
		setIcon(data.getIcon());
		setHorizontalTextPosition(SwingConstants.RIGHT);
		setIconTextGap(5);
		setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));

		//设置项目被选中时的颜色
		if (isSelected) {
			setBackground(list.getSelectionBackground());
			setForeground(list.getSelectionForeground());
		} else {
			setBackground(list.getBackground());
			setForeground(list.getForeground());
		}

		setEnabled(list.isEnabled());
		setFont(list.getFont());
		setOpaque(true);

		return this;
	}

}



